﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using SqlserverHelper;
using System.Threading;

using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using OJ;
using DLLmySQL;

public partial class login : System.Web.UI.Page
{

    public string strCnn = ConfigurationSettings.AppSettings["DataBaseString"];


    protected void Page_Load(object sender, EventArgs e)
    {
        string username = "";
        string password = "";
        if (Request.Form["username"] != null && Request.Form["username"].ToString().Trim() != "")
        {
            username = Request.Form["username"].ToString();
        }

        if (Request.Form["password"] != null && Request.Form["password"].ToString().Trim() != "")
        {
            password = Request.Form["password"].ToString();
        }


        Define.retUser ret = null;

        if (username == "" || password == "")
        {
            ret = new Define.retUser();
            ret.code = 40001;
            ret.msg = "缺少所需参数";

            Response.Write(Ojson.ObjectToJson(ret));
            return;
        }

        string stResult = f_user(username, password);
        Response.Write(stResult);
    }


    public string f_user(string username, string password)
    {
        Define.retUser ret = new Define.retUser();
       

        try
        {
            string strQuery = "select id,username,password,depart_id,user_identity from dsh_user where username =  '" + username.Trim() + "' and password = '" + password.Trim() + "' and login_year >= YEAR(now())  ";
            DataTable DT = CmySQL.GetDataTbale(strCnn, strQuery);

            if (DT != null && DT.Rows.Count > 0)
            {
                Define.User r = new Define.User();
                r.id = DT.Rows[0][0].ToString().Trim();
                r.username = DT.Rows[0][1].ToString().Trim();
                r.password = DT.Rows[0][2].ToString().Trim();
                r.depart_id = DT.Rows[0][3].ToString().Trim();
                r.user_identity = DT.Rows[0][4].ToString().Trim();

                ret.code = 200;
                ret.msg = "success";
                ret.data = r;

                CmySQL.GetDataTbale(strCnn, "update dsh_user set login_count = login_count + 1 , login_date = now() where  username =  '" + username.Trim() + "' and password = '" + password.Trim() + "'");

                OperLog.log("用户登录", r.user_identity);

            }
            else
            {
                ret.code = 40003;
                ret.msg = "未找到相关用户";
                ret.data = null;
            }
        }
        catch
        {
            ret.code = 20000;
            ret.msg = "服务不可用";
            ret.data = null;
        }

        return Ojson.ObjectToJson(ret);
    }
}